package cn.stylefeng.guns.modular.Dishes.service.Impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.enums.CommonStatusEnum;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.exception.enums.StatusExceptionEnum;
import cn.stylefeng.guns.modular.Dishes.entity.Dishes;
import cn.stylefeng.guns.modular.Dishes.mapper.dishesMapper;
import cn.stylefeng.guns.modular.Dishes.param.dishesParam;
import cn.stylefeng.guns.modular.Dishes.service.dishesService;
import cn.stylefeng.guns.modular.User.enums.UserEnum;
import cn.stylefeng.guns.sys.modular.emp.service.SysEmpService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class dishesServiceImpl extends ServiceImpl<dishesMapper, Dishes> implements dishesService {

    @Resource
    private SysEmpService sysEmpService;

    @Resource
    private dishesMapper dishesMapper;

    @Override
    public List<Dishes> list(dishesParam userOrgParam){
        LambdaQueryWrapper<Dishes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(Dishes::getStatus, CommonStatusEnum.DELETED.getCode());
        return this.list(queryWrapper);
    }

    @Override
    public void add(dishesParam userOrgParam) {
        Dishes dishes = new Dishes();
        BeanUtil.copyProperties(userOrgParam,dishes);
        this.save(dishes);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(dishesParam dishesParam) {
        Dishes dishes = this.queryCanteenUser(dishesParam);


        dishes.setStatus(CommonStatusEnum.DELETED.getCode());
        this.updateById(dishes);
        Long id = dishes.getId();
        //删除该用户对应的员工表信息
        sysEmpService.deleteEmpInfoByUserId(id);

        this.removeById(id);
    }

    @Override
    public void changeStatus(dishesParam dishesParam) {
        Long id = dishesParam.getId();
        Integer status = dishesParam.getStatus();

        // 校验状态在不在枚举值里
        CommonStatusEnum.validateStatus(status);

        // 更新枚举，更新只能更新未删除状态的
        LambdaUpdateWrapper<Dishes> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .eq(Dishes::getId,id)
                .and(i->i.ne(Dishes::getStatus,CommonStatusEnum.DELETED.getCode()))
                .set(Dishes::getStatus,status);
        boolean update = this.update(updateWrapper);
        if (!update) {
            throw new ServiceException(StatusExceptionEnum.UPDATE_STATUS_ERROR);
        }
    }

    @Override
    public void edit(dishesParam dishesParam) {
        Dishes dishes = this.queryCanteenUser(dishesParam);
        BeanUtil.copyProperties(dishesParam,dishes);
        dishes.setStatus(null);
        this.updateById(dishes);
    }

    @Override
    public List<Dishes> lookDishesScore(int day) {
        List<Dishes> list = dishesMapper.lookDishesSort(day);

        return list;
    }


    private Dishes queryCanteenUser(dishesParam dishesParam) {
        Dishes dishes = this.getById(dishesParam.getId());
        if (ObjectUtil.isEmpty(dishes)) {
            throw new ServiceException(UserEnum.NOT_EXISTED);
        }
        return dishes;

    }
}
